<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class ThreadCacheComponent extends Component
{

    public $_cache_config = 'threads';
    public $_page_num = 30;
    public $components = ['CategoryContentCache'];
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 获取文章信息
     */
    public function getVal($id)
    {
        $key = 'thread_' . $id;
        $data = Cache::read($key, $this->_cache_config);
        return empty($data) ? $this->createCache($id) : $data;
    }

    /**
     * 创建缓存
     */
    public function createCache($id)
    {
        $key = 'thread_' . $id;
        $controller = $this->_registry->getController();
        $controller->loadModel('Threads');
        $data = $controller->Threads->find()
            ->where(['id' => $id])
            ->first();
        $cache = empty($data) ? null : $data;
        Cache::write($key, $cache, $this->_cache_config);
        return $cache;
    }

    /**
     * 删除帖子缓存
     */
    public function deleteCache($id)
    {
        $key = 'thread_' . $id;
        Cache::delete($key, $this->_cache_config);
        return true;
    }

    /**
     * 删除帖子列表缓存
     */
    public function deleteList()
    {
        Cache::delete('list', $this->_cache_config);
        return true;
    }

    /**
     * 获取帖子列表
     */
    public function getList($data)
    {
        $key = md5(json_encode($data));
        $controller = $this->_registry->getController();
        $controller->loadModel('Threads');
        $cache = Cache::read('list', $this->_cache_config);
        if(empty($cache[$key])){
            $where = [];
            if(!empty($data['category_id'])){
                $controller->loadModel('CategoryContents');
                $category_where = [];
                $category_where['content_type'] = 'threads';
                $category_where['category_id'] = $data['category_id'];
                $category_content = $controller->CategoryContents->find()
                    ->select(['content_id'])
                    ->where($category_where)
                    ->order(['order_num desc'])
                    ->toArray();
                $where['id in'] = empty($category_content) ? [0] : array_column($category_content, 'content_id');
            }
            if(!empty($data['user_id'])){
                $where['user_id'] = $data['user_id'];
            }
            if(isset($data['display']) && is_numeric($data['display'])){
                $where['display'] = $data['display'];
            }
            if(isset($data['expert']) && is_numeric($data['expert'])){
//                $where['expert'] = $data['expert'];
                if($data['expert'] > 0){
                    $where['expert > '] = 0;
                }else{
                    $where['expert'] = 0;
                }
            }
            if(isset($data['expert >'])){
                $where['expert >'] = 0;
            }
            if(isset($data['essence']) && is_numeric($data['essence'])){
                $where['essence'] = $data['essence'];
            }
            if(isset($data['enabled']) && is_numeric($data['enabled'])){
                $where['enabled'] = $data['enabled'];
            }
            $alllist = $data['page'] == 'all' ? true : false;
            $page = (empty($data['page']) || $data['page'] == 'all') ? 1 :$data['page'];
            if(!empty($data['keyword'])){
                $where['title like'] = '%'.$data['keyword'].'%';
            }
            if($alllist){
                $thread = $controller->Threads->find()
                    ->select(['id'])
                    ->where($where);
            }else{
                $thread = $controller->Threads->find()
                    ->select(['id'])
                    ->where($where)
                    ->limit($this->_page_num)
                    ->page($page);
            }
            $cache[$key]['total'] = $thread->count();
            $thread = $thread->toArray();
            $cache[$key]['list'] = empty($thread) ? [] : array_column($thread, 'id');
            Cache::write('list', $cache, $this->_cache_config);
        }
        return $cache;
    }


    /**
     * 论坛帖子总数
     */
    public function getTotal()
    {
        $total = Cache::read('total', $this->_cache_config);
        if(empty($total)){
            $controller = $this->_registry->getController();
            $controller->loadModel('Threads');
            $total = $controller->Threads->find()
                ->select(['id'])
                ->where(['display=1 And enabled=1'])
                ->count();
            Cache::write('total', $total, $this->_cache_config);
        }
        return $total;
    }
}